define([
    'angular',
    'bootstrap-datepicker',
    'bootstrap-datepicker.zh-CN'
], function(angular) {
    var directive = angular.module('app.directive.datepicker', []);
    
    directive.directive('ngDatepicker', function() {
        var datepickerDefaults = {
            assumeNearbyYear: false,
            autoclose: true,
            calendarWeeks: false,
            clearBtn: true,
            toggleActive: false,
            daysOfWeekDisabled: [],
            daysOfWeekHighlighted: [],
            datesDisabled: [],
            endDate: Infinity,
            forceParse: true,
            format: 'yyyy-mm-dd',
            keepEmptyValues: false,
            keyboardNavigation: true,
            language: 'zh-CN',
            minViewMode: 0,
            maxViewMode: 4,
            multidate: false,
            multidateSeparator: ',',
            orientation: "auto",
            rtl: false,
            startDate: -Infinity,
            startView: 0,
            todayBtn: false,
            todayHighlight: true,
            updateViewDate: true,
            weekStart: 0,
            disableTouchKeyboard: false,
            enableOnReadonly: true,
            showOnFocus: true,
            dateCells:false,
            title: ''
        };
        return {
            restrict: 'A',
            scope: {
                ngDatepickerOptions: '='
            },
            link: function(scope, element, attrs) {
                var dpOptions = angular.extend(angular.copy(datepickerDefaults, {}),
                        angular.isDefined(scope.ngDatepickerOptions) ? (function(options) {
                            if (angular.isDefined(options.startDate)) {
                                options.initStartDate = options.startDate = eval(options.startDate);
                            }
                            if (angular.isDefined(options.endDate)) {
                                options.initEndDate = options.endDate = eval(options.endDate);
                            }
                            return options;
                        })(scope.ngDatepickerOptions) : {});
                element.datepicker(dpOptions).on('changeDate', function(e) {
                    if (angular.isDefined(dpOptions.startDateId)) {
                        angular.element('[ng-datepicker="' + dpOptions.startDateId + '"]').datepicker('setEndDate', e.date);
                    }
                    if (angular.isDefined(dpOptions.endDateId)) {
                        angular.element('[ng-datepicker="' + dpOptions.endDateId + '"]').datepicker('setStartDate', e.date);
                    }
                }).on('clearDate', function(e) {
                    if (angular.isDefined(dpOptions.startDateId)) {
                        var endDate = Infinity;
                        if (angular.isDefined(dpOptions.initEndDate)) {
                            endDate = dpOptions.initEndDate;
                        }
                        angular.element('[ng-datepicker="' + dpOptions.startDateId + '"]').datepicker('setEndDate', endDate);
                    }
                    if (angular.isDefined(dpOptions.endDateId)) {
                        var startDate = -Infinity;
                        if (angular.isDefined(dpOptions.initStartDate)) {
                            startDate = dpOptions.initStartDate;
                        }
                        angular.element('[ng-datepicker="' + dpOptions.endDateId + '"]').datepicker('setStartDate', startDate);
                    }
                });
            }
        };
    });

    return directive;
});